package cz.acies.genea.data.gedcom.model;

import java.util.Collections;
import java.util.Map;

public abstract class ExtensionContainer implements Visitable
{
	private Extensions extensions = null;

	public Map<String, Object> getExtensions() {
		return extensions != null ? extensions.getExtensions() : Collections.<String, Object> emptyMap();
	}

	public Object getExtension(String key) {
		return extensions != null ? extensions.get(key) : null;
	}

	public void setExtensions(Map<String, Object> exts) {
		if (exts != null && exts.size() > 0) {
			if (extensions == null) {
				extensions = new Extensions();
			}
			extensions.setExtensions(exts);
		} else {
			extensions = null;
		}
	}

	public void putExtension(String id, Object extension) {
		if (extensions == null) {
			extensions = new Extensions();
		}
		extensions.put(id, extension);
	}

	public void visitContainedObjects(Visitor visitor) {
		for (Map.Entry<String, Object> entry : getExtensions().entrySet()) {
			visitor.visit(entry.getKey(), entry.getValue());
		}
	}
}
